package com.letv.android.client.album.controller;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.letv.android.client.activity.AlbumPlayActivity;
import com.letv.android.client.activity.AlbumPlayRoomActivity;
import com.letv.android.client.view.LongWatchNoticeDialog;
import com.letv.android.client.view.LongWatchNoticeDialog.DismissCallBack;
import com.letv.core.bean.TipMapBean.TipBean;
import com.letv.core.utils.BaseTypeUtils;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.StatisticsUtils;
import com.letv.core.utils.TipUtils;
import com.letv.datastatistics.util.PageIdConstant;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 长时间观看工具类
 * 
 * @author zhuqiao
 *
 */
public class LongWatchController {
    /**
     * 长时观看的提醒时间.默认3小时
     */
    public static final long DEFAULT_LONG_WATCH_PERIOD = 3 * 60 * 60 * 1000;
    private long longWatchPeriod = DEFAULT_LONG_WATCH_PERIOD;

    private Context mContext;

    private Timer mWatchTimer = null;// 长时间观看定时器
    private TimerTask mTimerTask = null;

    private Handler mHandler = new Handler();

    public LongWatchController(Context context) {
        mContext = context;

        if (longWatchPeriod == DEFAULT_LONG_WATCH_PERIOD) {
            TipBean tipBean = TipUtils.getTipBean(mContext,"100029");
            if (!(tipBean == null || TextUtils.isEmpty(tipBean.getMessage()))) {
                LogInfo.log("zhuqiao", "long watch period:" + tipBean.getMessage());
                longWatchPeriod = (long) (BaseTypeUtils.stof(tipBean.getMessage(), 3) * 60 * 60 * 1000);
            }
        }

        startLongWatchCountDown();
    }

    /**
     * 开始进行长时间播放提醒
     */
    public void startLongWatchCountDown() {
        if (mTimerTask != null) {
            mTimerTask.cancel();
        }

        if (mWatchTimer != null) {
            mWatchTimer.cancel();
        }

        mWatchTimer = new Timer();
        mTimerTask = new TimerTask() {

            @Override
            public void run() {
                mHandler.post(new Runnable() {

                    @Override
                    public void run() {
                        boolean isPlaying = false;
                        if (mContext instanceof AlbumPlayActivity
                                && ((AlbumPlayActivity) mContext).getAlbumPlayFragment() != null) {
                            isPlaying = ((AlbumPlayActivity) mContext).getAlbumPlayFragment().isPlaying();
                        } else if(mContext instanceof AlbumPlayRoomActivity
                                && ((AlbumPlayRoomActivity) mContext).getAlbumPlayFragment() != null) {
                            isPlaying = ((AlbumPlayRoomActivity) mContext).getAlbumPlayFragment().isPlaying();
                        }

                        if (isPlaying) {
                            onLongWatch();
                        }
                    }
                });
            }
        };

        try {
            mWatchTimer.schedule(mTimerTask, longWatchPeriod);
        } catch (Exception e) {
            e.printStackTrace();
            if (mContext instanceof AlbumPlayActivity) {
                AlbumPlayActivity.sIsShowingLongwatch = false;
            } else if(mContext instanceof AlbumPlayRoomActivity) {
                AlbumPlayRoomActivity.sIsShowingLongwatch = false;
            }

            if (mWatchTimer != null) {
                mWatchTimer.cancel();
                mWatchTimer = null;
            }
            if (mTimerTask != null) {
                mTimerTask.cancel();
                mTimerTask = null;
            }
        }
    }

    /**
     * 主动消除长时间观看dialog
     */
    public void cancelLongTimeWatch() {
        if (mContext instanceof AlbumPlayActivity) {
            if (AlbumPlayActivity.sIsShowingLongwatch) {
                LongWatchNoticeDialog.dismissDialog();
                AlbumPlayActivity.sIsShowingLongwatch = false;
            }
        } else if(mContext instanceof AlbumPlayRoomActivity) {
            if (AlbumPlayRoomActivity.sIsShowingLongwatch) {
                LongWatchNoticeDialog.dismissDialog();
                AlbumPlayRoomActivity.sIsShowingLongwatch = false;
            }
        }

        startLongWatchCountDown();
    }

    private void onLongWatch() {
        if (mContext instanceof AlbumPlayActivity) {
            final AlbumPlayActivity activity = (AlbumPlayActivity) mContext;

            LogInfo.log("zhuqiao", "onLongWatch");
            AlbumPlayActivity.sIsShowingLongwatch = true;

            if (activity.getController() != null) {
                activity.getController().pause(false);
            }

            LongWatchNoticeDialog.show(new DismissCallBack() {

                @Override
                public void dimiss() {
                    startLongWatchCountDown();
                    if (activity.getController() != null) {
                        activity.getController().start();
                    }

                    AlbumPlayActivity.sIsShowingLongwatch = false;
                }
            }, mContext);
        } else if(mContext instanceof AlbumPlayRoomActivity) {
            final AlbumPlayRoomActivity activity = (AlbumPlayRoomActivity) mContext;

            LogInfo.log("zhuqiao", "onLongWatch");
            AlbumPlayRoomActivity.sIsShowingLongwatch = true;

            if (activity.getController() != null) {
                activity.getController().pause(false);
            }

            LongWatchNoticeDialog.show(new DismissCallBack() {

                @Override
                public void dimiss() {
                    startLongWatchCountDown();
                    if (activity.getController() != null) {
                        activity.getController().start();
                    }

                    AlbumPlayRoomActivity.sIsShowingLongwatch = false;
                }
            }, mContext);
        } else {
            return;
        }

        // 统计
        LogInfo.LogStatistics("long watch");
        StatisticsUtils.staticticsInfoPost(mContext, "19", "c68", null, 6, null, PageIdConstant.fullPlayPage, null,
                null, null, null, null);
    }

    public void onDestory() {
        if (mTimerTask != null) {
            mTimerTask.cancel();
        }

        if (mWatchTimer != null) {
            mWatchTimer.cancel();
        }
    }
}
